setwd("/home/jc227089/SRE/KB/DispOnly/limits/data")

fname<-"Kbyl_limits"
flist<-list.files(pattern=fname)

concat<-c()
for (ii in 1:length(flist)) {
	load(flist[ii])
	for (jj in 1:length(out)){
		if (is.null(out[[jj]]$pop)) next
		meanvec<-apply(out[[jj]]$pop, 2, mean)
		xmin<--min(out[[jj]]$pop[,"X"])
		xmax<-max(out[[jj]]$pop[,"X"])
		pars<-unlist(out[[jj]]$parameters)
		concat<-rbind(concat, c(pars, meanvec, lim=0.5*(xmin+xmax)))
	}
}

zmat<-tapply(concat[,"lim"], list(K=concat[,"K"], Lambda=concat[,"lambda"]), mean)
#zmat[is.na(zmat)]<-0

pdf(file=paste(fname, ".pdf", sep=""))
contour(x=as.numeric(levels(as.factor(concat[,"K"]))), y=as.numeric(levels(as.factor(concat[,"lambda"]))), z=zmat, xlab="K", ylab="Lambda", main=paste("b=", concat[1,"b"], sep=""))
dev.off()

save(concat, file="Trial2.RData")